import 'dart:math';

import 'package:flutterlib/common/datacenter/user_center.dart';


class Poi {

  Poi({
    this.cityCode,
    this.cityName,
    this.provinceName,
    this.title,
    this.adName,
    this.provinceCode,
    this.latitude,
    this.longitude,
  });

  Poi.fromJsonMap(Map<String, dynamic> map):
        cityCode = map['cityCode'] as String,
        cityName = map['cityName'] as String,
        provinceName = map['provinceName'] as String,
        title = map['title'] as String,
        adName = map['adName'] as String,
        provinceCode = map['provinceCode'] as String,
        latitude = map['latitude'] as String,
        longitude = map['longitude'] as String;

  String cityCode;
  String cityName;
  String provinceName;
  String title;
  String adName;
  String provinceCode;
  String latitude;
  String longitude;

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['cityCode'] = cityCode;
    data['cityName'] = cityName;
    data['provinceName'] = provinceName;
    data['title'] = title;
    data['adName'] = adName;
    data['provinceCode'] = provinceCode;
    data['latitude'] = latitude;
    data['longitude'] = longitude;
    return data;
  }
}

class DistanceUtil{

  static double getDistance(Poi pointA, Poi pointB){
    var dx= double.tryParse(pointA.latitude)-double.tryParse(pointB.latitude);
    var dy= double.tryParse(pointA.longitude)-double.tryParse(pointB.longitude);
    return sqrt(dx*dx+dy*dy);
  }

  static String getMyDistanceFrom(Poi pointMy, Poi point){
    if(pointMy == null || point == null) return '未知';
    var number = getDistance(pointMy, point);
    // String suffix.
    var abbr = '';
    var value = number;
    var absolute = value.abs();
    // If number > 1 trillion.
    if (absolute >= 1000000000000) {
      value = number / 1000000000000;
      abbr = 'TM';

      // If number > 1 billion.
    } else if (absolute >= 1000000000) {
      value = number / 1000000000;
      abbr = 'BM';

      // If number > 1 million.
    } else if (absolute >= 1000000) {
      value = number / 1000000;
      abbr = 'MM';

      // If number > 1 thousand.
    } else if (absolute >= 1000) {
      value = number / 1000;
      abbr = 'KM';
    }

    return _removeEndsZore(value.toStringAsFixed(1)) + abbr;

  }

  static String _removeEndsZore(String value) {
    if (value.length == 1) {
      return value;
    } else if (value.endsWith('.')) {
      return value.substring(0, value.length - 1);
    } else if (value.endsWith('0')) {
      return _removeEndsZore(value.substring(0, value.length - 1));
    }

    return value;
  }


  static Poi getPointFromStr(String location) {
    if (location.contains(",")) {
      var split = location.split(",");
      return Poi(longitude: split[0], latitude: split[1]);
    } else {
      return null;
    }
  }
}